package com.zjpavt.client.artnet.device;

/**
 * 灯光效果辅助类
 * @author zyc
 */
public class EffectUtil {

    /**
     * 单灯跑马灯
     * @param stripLight 灯带
     * @param backGround 背景色
     * @param runColor 跑动的灯的颜色
     * @param variate 变量
     * @return
     */
    public static byte[] waterfallEffect(StripLight stripLight, byte[] backGround, byte[] runColor, int variate) {
        int channelNum = stripLight.getSingleLightChannelSize();
        int lightNum = stripLight.getLightNum();
        if (channelNum != backGround.length || channelNum != runColor.length) {
            return null;
        }
        for (int index = 0; index < stripLight.getLightNum(); index++) {
            if (variate % 2 == index % 2) {
                stripLight.setLightColor(index, runColor);
            } else {
                stripLight.setLightColor(index, backGround);
            }
        }
        return stripLight.getAllData();
    }

    /**
     * 方波
     * @param stripLight
     * @param backGround 第一种颜色
     * @param runColor 第二种颜色
     * @param variate 变量
     * @param kuandu    方波总宽度
     * @param xiangwei 方波起始相位
     * @return
     */
    public static byte[] waterfallEffect(StripLight stripLight, byte[] backGround, byte[] runColor, int variate,int kuandu,int xiangwei) {
        int channelNum = stripLight.getSingleLightChannelSize();
        int lightNum = stripLight.getLightNum();
        if (channelNum != backGround.length || channelNum != runColor.length) {
            return null;
        }
        for (int index = 0; index < stripLight.getLightNum(); index++) {
            if (1  ==  (index + xiangwei +variate) /kuandu % 2) {
                stripLight.setLightColor(index, runColor);
            } else {
                stripLight.setLightColor(index, backGround);
            }
        }
        return stripLight.getAllData();
    }

    /**
     * 正弦波
     * @param stripLight    灯带
     * @param runColor     灯色
     * @param variate   变量
     * @param speed     速度，一个周期几个灯
     * @param xiangwei  相位，起始
     * @return
     */
    public static byte[] waterfallSin(StripLight stripLight, byte[] runColor, int variate,double speed,double xiangwei) {
        int channelNum = stripLight.getSingleLightChannelSize();
        int lightNum = stripLight.getLightNum();
        if (channelNum != runColor.length) {
            return null;
        }
        for (int index = 0; index < stripLight.getLightNum(); index++) {
            double fudu = (Math.sin((Math.PI * 2 * index + variate + xiangwei) /speed) +1) /2;
            byte[] color = ColorConst.tiaoxiang(runColor, fudu);
            stripLight.setLightColor(index,color);
        }
        return stripLight.getAllData();
    }

    /**
     * 阶梯效果
     * @param stripLight
     * @param runColor
     * @param variate
     * @param speed
     * @param xiangwei
     * @return
     */
    public static byte[] waterfallJieti(StripLight stripLight, byte[] runColor, int variate,double speed,double xiangwei) {
        int channelNum = stripLight.getSingleLightChannelSize();
        int lightNum = stripLight.getLightNum();
        if (channelNum != runColor.length) {
            return null;
        }
        for (int index = 0; index < stripLight.getLightNum(); index++) {
            double fudu = (Math.PI * 2 * index + variate + xiangwei) /speed;
            byte[] color = ColorConst.tiaoxiang(runColor, fudu);
            stripLight.setLightColor(index,color);
        }
        return stripLight.getAllData();
    }

    /**
     * 返现流水灯
     * @param stripLight
     * @param backGround
     * @param runColor
     * @param variate
     * @return
     */
    public static byte[] waterfallEffect1(StripLight stripLight, byte[] backGround, byte[] runColor, int variate) {
        int channelNum = stripLight.getSingleLightChannelSize();
        int lightNum = stripLight.getLightNum();
        if (channelNum != backGround.length || channelNum != runColor.length) {
            return null;
        }
        for (int index = 0; index < stripLight.getLightNum(); index++) {
            if (variate % lightNum == stripLight.getLightNum() -index) {
                stripLight.setLightColor(index, runColor);
            } else {
                stripLight.setLightColor(index, backGround);
            }
        }
        return stripLight.getAllData();
    }

    /**
     * 常亮
     * @param stripLight
     * @param color
     * @return
     */
    public static byte[] sameColor(StripLight stripLight, byte[] color) {
        if (color.length != stripLight.getSingleLightChannelSize()) {
            return null;
        }
        for (SingleLight s :
                stripLight.getLightList()) {
            s.setColor(color);
        }
        return stripLight.getAllData();
    }
}
